//
//  ZFApplyInfoHttpRequest.m
//  HappilyLoan
//
//  Created by zainishenbian on 2017/4/7.
//  Copyright © 2017年 zongqingxiangqian. All rights reserved.
//

#import "ZFApplyInfoHttpRequest.h"
#import "ZFHeader.h"

@implementation ZFApplyInfoHttpRequest
//提交用户基本资料
+ (void)requestSubmitUserBaseInfo:(ZFUserBaseInfoModal *)userBaseInfo sucessBlock:(void(^)(NSDictionary *dict))successBlock failureBlock:(void(^)(NSInteger failSign))failureBlock;
{
    //消耗时间
    NSString *consume_time = [ZFApplyInfoHttpRequest calculateConsumeTime:userBaseInfo.startTime];
    
    
    NSDictionary *bodyParameter = @{@"ad_code":userBaseInfo.ad_code,@"ad_path":userBaseInfo.ad_path,@"consume_time":consume_time,@"detail_address":userBaseInfo.detail_address,@"idcard_no":userBaseInfo.idcard_no,@"true_name":userBaseInfo.true_name,@"user_id":[ZFPulicAPI getUserid]};
    NSString *bodyStr = [ZFHttpTool sortBodyStr:bodyParameter];
    
    [ZFHttpTool postWithURL:[NSString stringWithFormat:@"%@/archives/postUserBasicInfo",SERVERIP] params:bodyParameter bodyStr:bodyStr success:^(id responseObject) {
        
        if ([responseObject[@"code"] integerValue] == 1) {
            
            NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
            [userDefaults setObject:responseObject[@"data"][@"user_id"] forKey:@"userid"];
            [userDefaults setObject:responseObject[@"data"][@"info_progress"] forKey:@"info_progress"];
            [userDefaults setObject:responseObject[@"data"][@"phone"] forKey:@"phone"];
            if (responseObject[@"data"][@"user_name"] != [NSNull null]) {
                [userDefaults setObject:responseObject[@"data"][@"user_name"] forKey:@"user_name"];
            }
            [userDefaults synchronize];
        }
        successBlock(responseObject);
        NSLog(@"%@-----%@----%@",responseObject[@"msg"],responseObject,[[NSUserDefaults standardUserDefaults] objectForKey:@"info_progress"]);
    } failure:^(NSError *error) {
        
        failureBlock(1);
        NSLog(@"%@",error);
    }];
}
//提交用户社会资料
+ (void)requestSubmitUserSocialInfo:(ZFUserSocialInfomodal *)usersocialInfo sucessBlock:(void(^)(NSDictionary *dict))successBlock failureBlock:(void(^)(NSInteger failSign))failureBlock;
{
    //消耗时间
    NSString *consume_time = [ZFApplyInfoHttpRequest calculateConsumeTime:usersocialInfo.startTime];
    
    NSDictionary *bodyparameter = @{@"consume_time":consume_time,@"edu_level":usersocialInfo.edu_level,@"own_credit":usersocialInfo.own_credit,@"social_identity":usersocialInfo.social_identity,@"user_id":[ZFPulicAPI getUserid],@"marry_state":usersocialInfo.marry_state,@"house_state":usersocialInfo.house_state};
    NSString *bodyStr = [ZFHttpTool sortBodyStr:bodyparameter];

    [ZFHttpTool postWithURL:[NSString stringWithFormat:@"%@/archives/postUserSocialInfo",SERVERIP] params:bodyparameter bodyStr:bodyStr success:^(id responseObject) {
        
        if ([responseObject[@"code"] integerValue] == 1) {
            
            NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
            [userDefaults setObject:responseObject[@"data"][@"user_id"] forKey:@"userid"];
            [userDefaults setObject:responseObject[@"data"][@"info_progress"] forKey:@"info_progress"];
            [userDefaults setObject:responseObject[@"data"][@"phone"] forKey:@"phone"];
            if (responseObject[@"data"][@"user_name"] != [NSNull null]) {
                [userDefaults setObject:responseObject[@"data"][@"user_name"] forKey:@"user_name"];
            }
            [userDefaults synchronize];
        }
        successBlock(responseObject);
        NSLog(@"%@-----%@",responseObject[@"msg"],responseObject);
    } failure:^(NSError *error) {
        
        failureBlock(1);
        NSLog(@"%@",error);
    }];
}
//提交用户财务信息
+ (void)requestSubmitFinancelInfo:(ZFFinancelInfoModal *)financelInfo sucessBlock:(void(^)(NSDictionary *dict))successBlock failureBlock:(void(^)(NSInteger failSign))failureBlock
{
    //消耗时间
    NSString *consume_time = [ZFApplyInfoHttpRequest calculateConsumeTime:financelInfo.startTime];
    
    NSDictionary *bodyParameter = @{@"consume_time":consume_time,@"other_income":financelInfo.other_income,@"owe_loan":financelInfo.owe_loan,@"salary_income":financelInfo.salary_income,@"user_id":[ZFPulicAPI getUserid]};
    
    NSString *bodyStr = [ZFHttpTool sortBodyStr:bodyParameter];
    
    [ZFHttpTool postWithURL:[NSString stringWithFormat:@"%@/archives/postUserFinancelInfo",SERVERIP] params:bodyParameter bodyStr:bodyStr success:^(id responseObject) {
        
//        if ([responseObject[@"code"] integerValue] == 1) {
        
//            NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
//            [userDefaults setObject:responseObject[@"data"][@"user_id"] forKey:@"userid"];
//            [userDefaults setObject:responseObject[@"data"][@"info_progress"] forKey:@"info_progress"];
//            [userDefaults setObject:responseObject[@"data"][@"phone"] forKey:@"phone"];
//            if (responseObject[@"data"][@"user_name"] != [NSNull null]) {
//                [userDefaults setObject:responseObject[@"data"][@"user_name"] forKey:@"user_name"];
//            }
//            [userDefaults synchronize];
//        }
        successBlock(responseObject);

        NSLog(@"%@",responseObject);
    } failure:^(NSError *error) {
        
        failureBlock(1);
    }];

}
//提交紧急联系人资料
+ (void)requestSubmitContactInfo:(NSArray *)contactarray sucessBlock:(void(^)(NSDictionary *dict))successBlock failureBlock:(void(^)(NSInteger failSign))failureBlock
{
    //消耗时间
    NSString *consume_time = nil;

    NSMutableArray *listArray = [NSMutableArray new];
    for (ZFContactInfoModal *infomodal in contactarray) {
        
        NSString *str = [NSString stringWithFormat:@"{\"ad_code\":%@,\"ad_path\":\"%@\",\"contact_name\":\"%@\",\"contact_phone\":\"%@\",\"contact_relation\":%@,\"contact_type\":%@,\"detail_address\":\"%@\",\"id\":%@}",infomodal.ad_code,infomodal.ad_path,infomodal.contact_name,infomodal.contact_phone,infomodal.contact_relation,infomodal.contact_type,infomodal.detail_address,infomodal.urgent_info_id];
        [listArray addObject:str];
        consume_time = [ZFApplyInfoHttpRequest calculateConsumeTime:infomodal.startTime];
    }
    NSString *listStr = [NSString stringWithFormat:@"[%@,%@]",listArray[0],listArray[1]];
    NSString *listUTF8 = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL,(CFStringRef)listStr,NULL,(CFStringRef)@"!*'();:@&=+$,/?%#[]",                                                                                               kCFStringEncodingUTF8 ));
    listUTF8 = [ZFApplyInfoHttpRequest blankEncode:listUTF8];
    NSDictionary *bodyParameter = @{@"user_id":[ZFPulicAPI getUserid],@"userUrgentInfoList":listStr,@"consume_time":consume_time};
    NSString *bodyUTF8 = [NSString stringWithFormat:@"consume_time=%@&userUrgentInfoList=%@&user_id=%@",consume_time,listUTF8,[ZFPulicAPI getUserid]];
    
    [ZFHttpTool postWithURL:[NSString stringWithFormat:@"%@/archives/postUserUrgentInfo",SERVERIP] params:bodyParameter bodyStr:bodyUTF8 success:^(id responseObject) {
        
        successBlock(responseObject);
    } failure:^(NSError *error) {
        
        failureBlock(1);
        NSLog(@"%@",error);
    }];

}
//提交用户职业信息
+ (void)requestSubmitProfessionInfo:(ZFUserProfessionInfoModal *)professionInfo sucessBlock:(void(^)(NSDictionary *dict))successBlock failureBlock:(void(^)(NSInteger failSign))failureBlock;
{
    //消耗时间
    NSString *consume_time = [ZFApplyInfoHttpRequest calculateConsumeTime:professionInfo.startTime];

    NSDictionary *bodyParameter = @{@"ad_code":professionInfo.ad_code,@"ad_path":professionInfo.ad_path,@"company_name":professionInfo.company_name,@"consume_time":consume_time,@"detail_address":professionInfo.detail_address,@"hire_date":professionInfo.hire_date,@"job_title":professionInfo.job_title,@"telephone":professionInfo.telephone,@"user_id":[ZFPulicAPI getUserid]};
    
    NSString *bodyStr = [ZFHttpTool sortBodyStr:bodyParameter];
    
    [ZFHttpTool postWithURL:[NSString stringWithFormat:@"%@/archives/postUserJobInfo",SERVERIP] params:bodyParameter bodyStr:bodyStr success:^(id responseObject) {
        
        successBlock(responseObject);

        NSLog(@"%@",responseObject);
    } failure:^(NSError *error) {
        
        failureBlock(1);
    }];
}
//提交用户银行卡信息
+ (void)requestSubmitUserBankCardInfo:(ZFUserBankCardInfoModal *)bankInfo smsCode:(NSString *)smsCode sucessBlock:(void(^)(NSDictionary *dict))successBlock failureBlock:(void(^)(NSInteger failSign))failureBlock
{
    //消耗时间
    NSString *consume_time = [ZFApplyInfoHttpRequest calculateConsumeTime:bankInfo.startTime];

    NSDictionary *bodyParameter = @{@"user_id":[ZFPulicAPI getUserid],@"owner_name":bankInfo.owner_name,@"bank_name":bankInfo.bank_name,@"bank_no":bankInfo.bank_no,@"bank_phone":bankInfo.bank_phone,@"sms_code":smsCode,@"consume_time":consume_time};
    NSString *bodyStr = [ZFHttpTool sortBodyStr:bodyParameter];

    [ZFHttpTool postWithURL:[NSString stringWithFormat:@"%@/archives/postUserBankcardInfo",SERVERIP] params:bodyParameter bodyStr:bodyStr success:^(id responseObject) {
        
        if ([responseObject[@"code"] integerValue] == 1) {
            
            NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
            [userDefaults setObject:responseObject[@"data"][@"user_id"] forKey:@"userid"];
            [userDefaults setObject:responseObject[@"data"][@"info_progress"] forKey:@"info_progress"];
            [userDefaults setObject:responseObject[@"data"][@"phone"] forKey:@"phone"];
            if (responseObject[@"data"][@"user_name"] != [NSNull null]) {
                [userDefaults setObject:responseObject[@"data"][@"user_name"] forKey:@"user_name"];
            }
            [userDefaults synchronize];
        }
        successBlock(responseObject);
        NSLog(@"%@-------%@",responseObject,responseObject[@"msg"]);
    } failure:^(NSError *error) {
        failureBlock(1);
        NSLog(@"%@",error);
    }];
}
//提交身份认证信息
+ (void)requestSubmitUserIDVerifyInfo:(ZFUserIDVerifyInfomodal *)IDVerifyInfo sucessBlock:(void(^)(NSDictionary *dict))successBlock failureBlock:(void(^)(NSInteger failSign))failureBlock
{
    //消耗时间
    NSString *consume_time = [ZFApplyInfoHttpRequest calculateConsumeTime:IDVerifyInfo.startTime];
    
    NSDictionary *bodyParameter = @{@"user_id":[ZFPulicAPI getUserid],@"consume_time":consume_time};

    NSString *bodyStr = [NSString stringWithFormat:@"consume_time=%@&user_id=%@",consume_time,[ZFPulicAPI getUserid]];
    
    [ZFHttpTool uploadImageWithURL:[NSString stringWithFormat:@"%@/archives/postUserIdentityInfo",SERVERIP] params:bodyParameter imgArray:IDVerifyInfo.picArray bodyStr:bodyStr success:^(id responseObject) {
        
        if ([responseObject[@"code"] integerValue] == 1) {
            
            NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
            [userDefaults setObject:responseObject[@"data"][@"user_id"] forKey:@"userid"];
            [userDefaults setObject:responseObject[@"data"][@"info_progress"] forKey:@"info_progress"];
            [userDefaults setObject:responseObject[@"data"][@"phone"] forKey:@"phone"];
            if (responseObject[@"data"][@"user_name"] != [NSNull null]) {
                [userDefaults setObject:responseObject[@"data"][@"user_name"] forKey:@"user_name"];
            }
            [userDefaults synchronize];
        }
        successBlock(responseObject);
        NSLog(@"%@-----%@",responseObject,responseObject[@"msg"]);
    } failure:^(NSError *error) {
        failureBlock(1);
        NSLog(@"%@",error);
    }];
}
//查询芝麻授权是否完成
+ (void)requestCheckZMAuthorizeStatusComplete:(void(^)(NSDictionary *dict))successBlock failureBlock:(void(^)(NSInteger failSign))failureBlock
{
    NSDictionary *bodyParameter = @{@"user_id":[ZFPulicAPI getUserid]};
    NSString *bodyStr = [ZFHttpTool sortBodyStr:bodyParameter];

    [ZFHttpTool postWithURL:[NSString stringWithFormat:@"%@/credit/findZmxyAuthForJSD",SERVERIP] params:bodyParameter bodyStr:bodyStr success:^(id responseObject) {
        
        if ([responseObject[@"code"] integerValue] == 1) {
            
            NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
            [userDefaults setObject:responseObject[@"data"][@"user_id"] forKey:@"userid"];
            [userDefaults setObject:responseObject[@"data"][@"info_progress"] forKey:@"info_progress"];
            [userDefaults setObject:responseObject[@"data"][@"phone"] forKey:@"phone"];
            if (responseObject[@"data"][@"user_name"] != [NSNull null]) {
                [userDefaults setObject:responseObject[@"data"][@"user_name"] forKey:@"user_name"];
            }
            [userDefaults synchronize];
        }

        successBlock(responseObject);
        NSLog(@"%@---%@",responseObject,responseObject[@"msg"]);
    } failure:^(NSError *error) {
        
        failureBlock(1);
        NSLog(@"%@",error);
    }];
}
//获取芝麻信用授权链接
+ (void)requestSesameCreditUrlSucessBlock:(void(^)(NSDictionary *dict))successBlock failureBlock:(void(^)(NSInteger failSign))failureBlock
{
    NSDictionary *bodyParameter = @{@"user_id":[ZFPulicAPI getUserid]};
    NSString *bodyStr = [ZFHttpTool sortBodyStr:bodyParameter];

    [ZFHttpTool postWithURL:[NSString stringWithFormat:@"%@/credit/getZmxyAuthEngineForJSD",SERVERIP] params:bodyParameter bodyStr:bodyStr success:^(id responseObject) {
        
        if ([responseObject[@"code"] integerValue] == 1) {
            
        }
        successBlock(responseObject);
        NSLog(@"%@-------%@",responseObject,responseObject[@"msg"]);
    } failure:^(NSError *error) {
        
        failureBlock(1);
        NSLog(@"%@",error);
    }];
}
#pragma mark ———— 计算消耗时间
+ (NSString *)calculateConsumeTime:(NSString *)startTime
{
    NSString *endTime = [ZFPulicAPI getCurrentTimestamp];
    NSString *consume_time = [NSString stringWithFormat:@"%ld",[endTime integerValue] - [startTime integerValue]];
    return consume_time;
}
+ (NSString *)blankEncode:(NSString *)str
{
    return [str stringByReplacingOccurrencesOfString:@"%20" withString:@"+"];
}
@end
